package com.huaboot.system.security.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.huaboot.framework.security.user.LoginUser;
import com.huaboot.system.convert.SysUserConvert;
import com.huaboot.system.entity.SysUserEntity;
import com.huaboot.system.service.SysUserDetailsService;
import com.huaboot.system.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

/**
 * description: UserDetailsServiceImpl
 * author: ff
 */

@Service
public class UserDetailsServiceImpl implements UserDetailsService {

    @Autowired
    private SysUserService sysUserService;

    @Autowired
    private SysUserDetailsService sysUserDetailsService;


    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        SysUserEntity sysUserEntity = sysUserService.getOne(new QueryWrapper<SysUserEntity>().eq("username", username));
        if (sysUserEntity == null) {
            throw new UsernameNotFoundException("用户名或密码错误");
        }
       // LoginUser loginUser = new LoginUser(sysUserEntity.getId(), sysUserEntity.getUsername(), sysUserEntity.getPassword(), null, sysUserEntity.getSuperAdmin());
        LoginUser loginUser = SysUserConvert.INSTANCE.convertDetail(sysUserEntity);
        loginUser.setUserId(sysUserEntity.getId());
        return sysUserDetailsService.getUserDetails(loginUser);
    }
}
